#include "AsyncTestSubsystem.h"

#include "Framework/Application.h"
#include "Async/Async.h"

#include <thread>
#include <iostream>

using namespace std;

void RAsyncTestSubsystem::Initialize()
{
    // QueueSchedule* MainThreadSchedule=new QueueSchedule();
    // CThreadPoolSchedule  * ThreadPoolSchedule=new CThreadPoolSchedule(2);

    std::vector<std::shared_ptr<CPackagedTaskBase>> Tasks;
    std::vector<CFuture<int>> Futures;



    // auto p=CPackagedTask<int>( ThreadPoolSchedule,[](){
    //         cout<<" thread id:"<<std::this_thread::get_id() <<endl;
    //     });

    for(int i=0; i<10;i++)
    {
        auto Task=std::make_shared<CPackagedTask<int>>( 
            RApplication::GetApplication()->Async->ThreadPool,
            [i](){
            cout<<i<<" thread id:"<<std::this_thread::get_id() <<endl;
            return i+10000;
        });

        Task->Then( RApplication::GetApplication()->Async->MainThread,
        std::function<int(int)>([i](int Result)->int{
            cout<<i<<"then in main thread "<<(Result+10000)<<endl;
            return Result+10000;
        }))
            ->Then(RApplication::GetApplication()->Async->ThreadPool,
            std::function<void(int)>(
            [i](int Result){
                cout<<i<<"then in sub thread "<<(Result+10000)<<endl;
            }))
        ;

        Tasks.push_back(Task);

        Futures.push_back(Task->GetFuture());

        Task->Task->AddToSchedule();
    }



}